﻿using FanaticGene.OpenSlide.Models;
using Microsoft.Office.Interop.PowerPoint;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Xml.Serialization;

namespace FanaticGene.OpenSlide.UserControls
{
    /// <summary>
    /// OptimizeFontDialogUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class OptimizeFontDialogUserControl : UserControl
    {
        public OptimizeFontDialogUserControl()
        {
            InitializeComponent();


            var chsFonts = new[] { "微软雅黑", "黑体", "宋体" };
            foreach (var ch in chsFonts) PART_ChsFontComboBox.Items.Add(ch);
            PART_ChsFontComboBox.SelectedIndex = 0;

            var engFonts = new[] { "Arial" };
            foreach (var en in engFonts) PART_EngFontComboBox.Items.Add(en);
            PART_EngFontComboBox.SelectedIndex = 0;
        }

        private void PART_ForcedModeRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if (IsInitialized == false) return;
            PART_SelectSlideStackPanel.Visibility = Visibility.Visible;
        }

        private void PART_ForcedModeRadioButton_Unchecked(object sender, RoutedEventArgs e)
        {
            if (IsInitialized == false) return;
            PART_SelectSlideStackPanel.Visibility = Visibility.Collapsed;
        }

        private void PART_ChsFontCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (IsInitialized == false) return;
            PART_ChsFontComboBox.IsEnabled = true;
        }

        private void PART_ChsFontCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if (IsInitialized == false) return;
            PART_ChsFontComboBox.IsEnabled = false;
        }

        private void PART_EngFontCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            if (IsInitialized == false) return;
            PART_EngFontComboBox.IsEnabled = true;
        }

        private void PART_EngFontCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            if (IsInitialized == false) return;
            PART_EngFontComboBox.IsEnabled = false;
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (IsInitialized == false) return;
            var isSelectedChs = PART_ChsFontCheckBox.IsChecked == true;
            var isSeelctedEng = PART_EngFontCheckBox.IsChecked == true;

            var chsFont = PART_ChsFontComboBox.SelectedItem as string;
            var engFont = PART_EngFontComboBox.SelectedItem as string;

            var isForced = PART_ForcedModeRadioButton.IsChecked == true;
            var isAll = PART_AllSlideRadioButton.IsChecked == true;
            var isEditHidden = PART_IncludeHiddenCheckBox.IsChecked == true;
            if (isForced)
            {
                var slds = GetSlides(isAll);
                ForceSetFont(chsFont, engFont, isSelectedChs, isSeelctedEng, slds, isEditHidden);
            }
            else
            {
                CreateAndLoadFontScheme(chsFont, engFont, isSelectedChs, isSeelctedEng);
            }

        }

        private IEnumerable<Slide> GetSlides(bool isAll)
        {
            var list = new List<Slide>();
            var app = Globals.ThisAddIn.Application;

            if (isAll)
            {
                var pres = app.ActivePresentation;
                var slds = pres.Slides;
                foreach (Slide slide in slds)
                {
                    list.Add(slide);
                }
            }
            else
            {
                var docwin = app.ActiveWindow;
                var sel = docwin.Selection;
                if (sel.Type == PpSelectionType.ppSelectionSlides || sel.Type == PpSelectionType.ppSelectionShapes)
                {
                    var slds = sel.SlideRange;
                    foreach (Slide slide in slds)
                    {
                        list.Add(slide);
                    }
                }
            }

            return list;
        }

        private void ForceSetFont(string chsFont, string engFont, bool isSelectedChs, bool isSelectedEng, IEnumerable<Slide> slides, bool isEditHidden)
        {
            foreach (Slide slide in slides)
            {
                if (slide.SlideShowTransition.Hidden == Microsoft.Office.Core.MsoTriState.msoTrue && isEditHidden == false) continue;
                foreach (Shape shape in slide.Shapes)
                {
                    var font = shape.TextFrame.TextRange.Font;
                    if (isSelectedEng)
                        font.NameAscii = engFont;
                    if (isSelectedChs)
                        font.NameFarEast = chsFont;
                }
            }
        }

        private void CreateAndLoadFontScheme(string chsFont, string engFont, bool isSelectedChs, bool isSelectedEng)
        {
            var dir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var tempPath = Path.Combine(dir, "tmp.xml");
            var tempPath2 = Path.Combine(dir, "tmp2.xml");
            var app = Globals.ThisAddIn.Application;
            var pres = app.ActivePresentation;
            var mast = pres.SlideMaster;
            var th = mast.Theme.ThemeFontScheme;
            th.Save(tempPath);

            var seri = new XmlSerializer(typeof(fontScheme));
            fontScheme fscheme = null;
            using (var fs = new FileStream(tempPath, FileMode.Open))
            {
                fscheme = seri.Deserialize(fs) as fontScheme;
            }

            if (isSelectedChs)
            {
                fscheme.minorFont.ea.typeface = chsFont;
                fscheme.majorFont.ea.typeface = chsFont;
            }

            if (isSelectedEng)
            {
                fscheme.minorFont.latin.typeface = engFont;
                fscheme.majorFont.latin.typeface = engFont;
            }


            using (var fs = new FileStream(tempPath2, FileMode.Create))
            {
                seri.Serialize(fs, fscheme);
            }

            File.Delete(tempPath);
            File.Delete(tempPath2);

            th.Load(tempPath2);


            //th.Load(tempPath);
        }

    }
}
